/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upf.grupE.impl;


import edu.upf.grupE.Listener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

/**
 *
 * @author xday666
 */
public class ChatListener implements Listener {
    /*this class implements the listener which will be prepared to understand
     the Chat Packets.*/
    private IM im;

    /**
     * Receives the IM object and initializes the object.
     * @param instancem The IM object
     */
    public ChatListener(final IM instancem) {
        im = instancem;
    }

    /**
     * Recives the packet. If its a Message prints it in the IM.addText method
     * @param packet The received Packet
     */
    public final void processPacket(final Packet packet) {
        if (packet instanceof Message) {
            /*in this messages there are only contained the Date object, but we
             are not using it on reception. Here what we to is extract the
             address of the sender of the packet and the body, and we print it
             on the Text Box.*/
            Message msg = (Message) packet;
            System.out.println(msg.getBody());

            im.el.recivedMessage(msg.getBody(), msg.getFrom());
            //im.addText(msg.getFrom(), msg.getBody());
        }
    }
}
